package com.active.api.impl;

import com.active.api.ActiveHandle;
import com.active.bean.ActiveInfo;
import com.active.bean.ActiveList;
import com.active.bean.ActiveTree;
import com.active.bean.ActiveWisdom;
import com.active.service.IActiveInfoService;
import com.active.service.IActiveListService;
import com.active.service.IActiveTreeService;
import com.active.service.IActiveWisdomService;
import com.base.dto.PageContent;
import com.base.exception.NormalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ActiveHandleImpl implements ActiveHandle {

    @Autowired
    private IActiveListService activeListService;

    @Autowired
    private IActiveInfoService activeInfoService;

    @Autowired
    private IActiveTreeService activeTreeService;

    @Autowired
    private IActiveWisdomService activeWisdomService;

    @Override
    @Transactional(readOnly = true)
    public List<ActiveList> refreshListView(int currentPage,int userId) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("userId",userId);
        PageContent<List<ActiveList>> page = new PageContent<>();
        page.setPageIndex(currentPage);
        page.setPageSize(10);
        page.setParam(param);
        PageContent<List<ActiveList>> obj = activeListService.getPage(page);
        return obj.getResultData();
    }

    @Override
    @Transactional(readOnly = true)
    public ActiveList getActiveListById(Integer id,int userId) throws Exception {
        return activeListService.getActiveListByUserAndId(id,userId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<ActiveInfo> getActiveNodes(Integer activeListId,int userId) throws Exception {
        return activeInfoService.selectByUserAndActiveListId(activeListId,userId);
    }

    @Override
    @Transactional(readOnly = false)
    public void startMission(Integer activeListId,int userId) throws Exception {
        ActiveList activeList = activeListService.getActiveListByUserAndId(activeListId,userId);
        List<ActiveInfo> activeInfos = activeInfoService.selectByUserAndActiveListId(activeListId,userId);
        if(activeInfos!=null){
            activeList.setActiveId(activeInfos.get(0).getId());
        }
        activeList.setNodeIndex(0);
        activeList.setState(1);
        activeListService. updateActiveListById(activeList);
    }

    @Override
    @Transactional(readOnly = false)
    public void stopMission(Integer activeListId,int userId) throws Exception {
        ActiveList activeList = activeListService.getActiveListByUserAndId(activeListId,userId);
        activeList.setNodeIndex(-1);
        activeList.setState(null);
        activeList.setActiveId(null);
        activeListService.updateActiveListById(activeList);
        activeInfoService.updateActiveInfoToStopByListId(activeListId);
    }

    @Override
    @Transactional(readOnly = false)
    public void nextMission(Integer activeListId, Integer state,int userId) throws Exception {
        ActiveList activeList = activeListService.getActiveListByUserAndId(activeListId,userId);
        if(activeList.getActiveId()==null) return ;
        ActiveInfo activeInfo = activeInfoService.getActiveInfoById(activeList.getActiveId());
        if(activeInfo==null) return;
        activeInfo.setState(state);
        activeInfo.setFinishedTime(new Date());
        ActiveTree activeTree = activeTreeService.getNextActiveTree(activeListId,activeList.getActiveId());
        if(activeTree==null){
            activeList.setState(2);
            activeList.setActiveId(null);
            Integer index = activeInfoService.checkMissionFinished(activeListId);
            if((index==null||index==0) && Objects.equals(state,1)){
                activeList.setHasFinished(1);
            }else{
                activeList.setHasFinished(0);
            }
        }else{
            activeList.setNodeIndex(activeInfo.getCurrentIndex()+1);
            activeList.setActiveId(activeTree.getSunId());
        }
        activeInfoService.updateActiveInfoById(activeInfo);
        activeListService.updateActiveListById(activeList);
    }

    @Override
    @Transactional(readOnly = false)
    public void prevMission(Integer activeListId,int userId) throws Exception {
        ActiveList activeList = activeListService.getActiveListByUserAndId(activeListId,userId);
        if(activeList.getActiveId()==null) return ;
        ActiveInfo activeInfo = activeInfoService.getActiveInfoById(activeList.getActiveId());
        if(activeInfo==null) return;
        activeInfo.setState(null);
        ActiveTree activeTree = activeTreeService.getPrevActiveTree(activeListId,activeList.getActiveId());
        activeList.setNodeIndex(activeList.getNodeIndex()-1);
        if(activeTree == null || activeTree.getParentId()==null){
            activeList.setState(null);
            activeList.setActiveId(null);
        }else{
            activeList.setActiveId(activeTree.getParentId());
            activeList.setNodeIndex(activeInfo.getCurrentIndex()-1);
        }
        activeInfoService.updateActiveInfoById(activeInfo);
        activeListService.updateActiveListById(activeList);
    }

    @Override
    @Transactional(readOnly = false)
    public int addMission(ActiveList mission, List<ActiveInfo> missionNodes,int userId) throws Exception {
        if(mission.getId()==null){
            mission.setUserId(userId);
            activeListService.insertActiveList(mission);
        }else{
            mission.setUserId(userId);
            activeListService.updateActiveListById(mission);
        }
        Integer parentId=null,sunId=null;
        int index=0;
        activeTreeService.deleteActiveTreeByActiveListId(mission.getId());
        activeInfoService.deleteActiveInfoByActiveListId(mission.getId());
        for(ActiveInfo activeInfo:missionNodes){
            activeInfo.setCurrentIndex(index++);
            activeInfo.setListId(mission.getId());
            activeInfoService.insertActiveInfo(activeInfo);
            sunId=activeInfo.getId();
            ActiveTree activeTree = new ActiveTree();
            activeTree.setParentId(parentId);
            activeTree.setSunId(sunId);
            activeTree.setListId(mission.getId());
            parentId=sunId;
            activeTreeService.insertActiveTree(activeTree);
        }
        return 1;
    }

    @Override
    @Transactional(readOnly = false)
    public int deleteMission(Integer activeListId,int userId) throws Exception {
        ActiveList activeList = activeListService.getActiveListByUserAndId(activeListId,userId);
        if(activeList == null) throw new NormalException("该条记录不存在！");
        activeTreeService.deleteActiveTreeByActiveListId(activeListId);
        activeInfoService.deleteActiveInfoByActiveListId(activeListId);
        activeListService.deleteActiveListById(activeListId);
        return 1;
    }

    @Override
    public List<ActiveWisdom> randomActiveWisdom(Integer num) throws Exception {
        return activeWisdomService.selectRandomRecord(num);
    }

}
